var util = require('../../utils/util');
var wxApi = require('../../utils/wxApi');
var wxRequest = require('../../utils/wxRequest');
import config from '../../utils/config';
var app = getApp();
var that;

//获取应用实例
var app = getApp()
Page({
  data: {
    user: {}
    ,pageInex:1
    ,isload:0
    ,tid:0
    ,team:null
    ,group:[]
    ,sgid:0
    ,showgroup:true
    ,userall:0
    ,stepall:0
    ,team_pm:[]
    ,group_index: 0

    ,isLoadMore : false
    ,isEnd : false
    ,isEmpty:false

    ,cjt:0
    ,yjt:0
    ,cjtstr:"距离比赛结束"
    ,wks:true
    ,rens:0
    ,list:[]
    ,rens:0
    ,step:0
    ,pageInex:1
    ,joingroup:false
    ,zpm:0
    ,tpm:0
    ,hasgroup:true
    ,gname:""
    ,gid :0
    ,zrs:0
    ,zbs:0
  },
  onShareAppMessage: function () {
      return app.getShare();
  },
  onShow:function(){
   
  },
  onReady:function(){
    that.data.isload=0;
    
  },
  onReachBottom : function(e,a){
    that.loadList();
  },
  onPullDownRefresh:function(){
    that.setData({
      list : [],
    });
    that.loadData();
  },
  onLoad: function (options) {
    that = this;
    that.data.isload=1;
    that.data.pageInex=1;
    if( !util.judge.isNull(options.tid) ){
      that.data.tid = parseInt(options.tid);
    }
    that.loadData();
  },
  
  loadData: function () {
    util.showLoading();
    app.getUserInfo()
    .then(res => {
        console.log(res);
      that.setData({
        user:res
      })
      
      var url = config.url_team_get;
      var params = {
        id:that.data.tid,
        code:''
      }
      return wxRequest.postRequest(url, params)
    })
    .then(res => {
        console.log(res);
        if(util.judge.isObject(res)){

          var team =res.team;
          that.data._sdesc="D几名邀请您参加\""+team.name+ "\"比赛！" ;
          wx.setNavigationBarTitle( { title: team.name });

          var bd = util.str2date(team.bdate);
          var ed = util.str2date(team.edate);

          res.team.bdate = util.date2str(team.bdate,"MM.dd");
          res.team.edate = util.date2str(team.edate,"MM.dd");

          var d1 = new Date();    //结束时间  
          if(team.isclose ==1){
              that.setData({cjtstr:"比赛已关闭",wks:false});
          }else{
              if(d1.getTime() > ed.getTime()){
                  that.setData({cjtstr:"比赛已结束",wks:false});
              }
              else if(bd.getTime() > d1.getTime()){
                  that.setData({cjtstr:"比赛未开始",wks:false});
              }
              else{
                  //进行中
                  var date3 = ed.getTime() - d1.getTime();   //时间差的毫秒数 
                  var days=parseFloat(date3/(24*3600*1000));  
                  if(days < 1){
                      that.setData({cjtstr:"比赛今天结束",wks:false});
                  }else{
                      days=that.modFoat(days);
                      that.setData({ cjtstr: "距离比赛结束", wks: true });
                  }
              }
          }
          date3 = ed.getTime() - bd.getTime();   //时间差的毫秒数        
          var yjt = parseInt(parseFloat(date3/(24*3600*1000) + 0.00001))+1; 

          that.setData({cjt:days,yjt:yjt});

          that.setData({
            team:res.team,
            stepall:res.stepall,
            userall:res.userall,
            team_pm:res.pm
          });

          that.loadList();
        }
    })
    .catch(res => {
        console.log(res);
        util.alert_catch(res.message);
    })
    .finally(function (res) {
        util.hideLoading()
        wx.stopPullDownRefresh();
    });
    
  },

  loadList: function(reload){
     
    that.setData({
      isLoadMore : true
    });

    var url = config.url_team_group_Pm;
    var id= that.data.tid;
    var params = {
      id:id
    }
    wxRequest.postRequest(url, params)
    .then(res=>{
        var irs=0;
        var ibs=0;
        for(var i in res){
            irs = irs +parseInt(res[i].cc);
            ibs = ibs +parseInt(res[i].stepall);
        }

        that.setData({
            list : res
            ,zrs:irs
            ,zbs:ibs
        });
    })
    .catch(res => {
      util.alert(res.message);
    })
    .finally(function (res) {
      util.hideLoading();
    });
  },

  modFoat:function(v) {
        var _max = parseInt(v) + 1;
        if( _max - v < 1 ) {
            return _max;
        }
        return v;
    } 

})